package managerSIP;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import net.sf.asterisk.manager.AuthenticationFailedException;
import net.sf.asterisk.manager.ManagerConnection;
import net.sf.asterisk.manager.ManagerConnectionFactory;
import net.sf.asterisk.manager.TimeoutException;
import net.sf.asterisk.manager.action.CommandAction;
import net.sf.asterisk.manager.action.OriginateAction;
import net.sf.asterisk.manager.response.CommandResponse;
import net.sf.asterisk.manager.response.ManagerResponse;

public class Conference {

    private ManagerConnection managerConnection;

    private ManagerResponse originateResponse;

    private CommandAction commandAction;

    private CommandResponse response;


    //<BETA>
    //New constructor receiving parameters
    public Conference(String hostname, String username, String password) throws
    IOException {
	// connect to Asterisk
	this.originateResponse = new ManagerResponse();
	this.commandAction = new CommandAction();
	this.response = new CommandResponse();
	try {
	    ManagerConnectionFactory factory = new ManagerConnectionFactory();
	    this.managerConnection = factory.getManagerConnection(hostname, username,
		    password);
	}
	catch (Exception e) {
	    e.printStackTrace();
	}
    }
    //</BETA>

    public Conference() {
	
    }

    public String inviteUser(String userName, int ConfNumber) throws IOException,
    AuthenticationFailedException,
    TimeoutException {
	// set the user and conference number
	System.out.println("inviteUser: " + userName + ConfNumber + " \n");

	OriginateAction originateAction = new OriginateAction();
	originateAction.setChannel("SIP/" + userName);
	originateAction.setExten(String.valueOf(ConfNumber));
	originateAction.setPriority(Integer.valueOf(1));
	originateAction.setTimeout(Long.valueOf(30000));

	// Test and connect to Asterisk and log in
	if (!managerConnection.isConnected()) {
	    managerConnection.login();
	}

	// send the originate action and wait 30 seconds for a Asterisk reply
	originateResponse = managerConnection.sendAction(
		originateAction, 30000);

	// log off asterisk
	//MgrAct.logoff();

	// return whether the originate succeeded or not
	return originateResponse.getResponse();

    }

    public void kickUser (String userIdConf, int ConfNumber) throws SQLException,
    IllegalArgumentException, IllegalStateException, IOException,
    AuthenticationFailedException, TimeoutException {

	commandAction.setCommand("meetme kick " + String.valueOf(ConfNumber) + " " +
		userIdConf);

	// Test and connect to Asterisk and log in
	if (!managerConnection.isConnected()) {
	    managerConnection.login();
	}

	// send the command action and wait 30 seconds for a Asterisk reply
	originateResponse = managerConnection.sendAction(
		commandAction, 30000);

	response = (CommandResponse) originateResponse;
	List list = response.getResult();
	for (Iterator<String> it = list.iterator(); it.hasNext(); ) {
	    String s = it.next();
	    System.out.println(s);
	}

	// log off asterisk
	//managerConnection.logoff();

    }

    public void printInfoUser (String userName) throws IOException, AuthenticationFailedException, TimeoutException
    {
	// Get user info
	// set user
	commandAction.setCommand("realtime load sipusers name " + userName);

	// Test and connect to Asterisk and log in
	if(!managerConnection.isConnected()){ 
	    managerConnection.login();
	}

	// send the command action and wait 30 seconds for a Asterisk reply
	originateResponse = managerConnection.sendAction(commandAction, 30000);

	response = (CommandResponse)originateResponse;
	List list = response.getResult();
	for (Iterator<String> it = list.iterator(); it.hasNext ();)
	{
	    String s = it.next();
	    System.out.println(s);
	}

	// log off asterisk
	//managerConnection.logoff();

    }

    public int countUsers(int ConfNumber) throws SQLException,
    IllegalArgumentException, IllegalStateException, IOException,
    AuthenticationFailedException, TimeoutException {
	// List users from this conference
	CommandAction commandAction = new CommandAction();
	commandAction.setCommand("meetme list " + String.valueOf(ConfNumber));

	// Test and connect to Asterisk and log in
	if (!managerConnection.isConnected()) {
	    managerConnection.login();
	}

	// send the command action and wait 30 seconds for a Asterisk reply
	originateResponse = managerConnection.sendAction(
		commandAction, 30000);

	response = (CommandResponse) originateResponse;

	// Parse results to a List and get his size by Objects
	List list = response.getResult();
	String stringResponse = list.toArray()[1].toString();

	// Returns index where begins that string
	int position = stringResponse.indexOf(" users in that conference.");

	// log off asterisk
	//managerConnection.logoff();

	// From the index to the position string
	return Integer.parseInt(stringResponse.substring(0, position));

    }

}
